export const postURL = "https://nantong.iceand.cn/api/"
// export const postURL = "http://127.0.0.1:8000/api/"
import Vue from 'vue'
const login_num ="nantong_login_num"
function setHeader(isfile){
	let token = uni.getStorageSync('nangtong_login_token');
	let header = {'content-type':'application/json'}
	if(token){
		header = {'Authorization':token,'content-type':'application/json'}
	}
	if(isfile){
		header = {'Authorization':token}
	}
	return header
}
//统一响应
function response(response,resolve,reject){
	uni.hideLoading();
	
	var data = response.data

	if(data.code==0){
		uni.showToast({
			title: data.message,
			icon: 'none',
			success() {
			}
		});
		
		return false
	}else if(data.code==3){
		var rout = data.data
		let login_num = uni.getStorageSync("nantong_login_num")
		uni.navigateTo({
			url:`/pages/login/index`
		})
		
		return false
	}else{
	
		return data	
	}
}
export const myPost = ((url,data)=>{

	return new Promise((resolve, reject)=>{
		uni.getNetworkType({
			success:function(res){
				if(res.networkType=="none"){
					uni.showToast({
						title:'当前无网络'
					})
					reject("当前无网络")
				}else{
			
					uni.showLoading()
					uni.request({
						url:postURL + url,
						method:"POST",
						data: data,
						header:setHeader(false),
						success:(res)=>{
							let r = response(res)
							if(!r){
								reject("请登录")
							}else{
							
								resolve(r)
							}
						},
						fail:(err)=>{
							uni.showToast({
								title:'请求接口失败',
								icon:"none"
							})
							reject(err)
						}
					})
				}
			
			}
		})
	
	})
	
})


export const myGet = function(url,data){
	return new Promise((resolve, reject)=>{
		uni.getNetworkType({
			success:function(res){
			
				if(res.networkType=="none"){
					uni.showToast({
						title:'当前无网络'
					})
					reject("当前无网络")
				}else{
					uni.showLoading()
					uni.request({
						url:postURL + url,
						method:"GET",
						data: data?data.data:{},
						header:setHeader(false),
						success:(res)=>{
							let r = response(res)
							if(!r){
								reject("请登录")
							}else{
							
								resolve(r)
							}
						},
						fail:(err)=>{
							uni.showToast({
								title:'请求接口失败'
							})
							reject(err)
						}
					})
				}
			
			}
		})
	
	})
}
export const uploadfile = function(url,data){
	return new Promise((resolve, reject)=>{
		uni.getNetworkType({
			success:function(res){
			
				if(res.networkType=="none"){
					uni.showToast({
						title:'当前无网络'
					})
					reject("当前无网络")
				}else{
					uni.showLoading()
					
					uni.uploadFile({
						url:postURL + url,
						filePath: data,
						name:"file",
						header:setHeader(true),
						success: (res) => {
	
							res.data = JSON.parse(res.data)
							let r = response(res)
							if(!r){
								reject("请登录")
							}else{
							
								resolve(r)
							}
						},
						fail:(err)=>{
							uni.showToast({
								title:'请求接口失败'
							})
							reject(err)
						}
					});
				
				}
			
			}
		})
	
	})
}
export function countDown (endtimes){
	var nowtime = new Date();
	var endtime = new Date(endtimes);
	var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
	var d = parseInt(lefttime/(60 * 60 * 24))
	
	var h = parseInt(lefttime/(60 * 60) );
	var m = parseInt(lefttime / 60 % 60);
	var s = parseInt(lefttime % 60);
	if(h>0){
		h=(h>=10?h:"0"+h)  
	}else{
		h = "00"
	}			
	if(m>0){
		m=(m>=10?m:"0"+m)  
	}else{
		m = "00"
	}
	if(s>0){
		s=(s>=10?s:"0"+s)  
	}else{
		s = "00"
	}
	
	return {d:d,h:h,m:m,s:s,lefttime:lefttime}
}

export function setRoles(roles){
    uni.setStorageSync("nantong_roles_array",roles)
  
}
export function getRoles(){
    let array = uni.getStorageSync("nantong_roles_array")
    return array || []
}
export default {myPost,myGet,postURL,countDown,uploadfile,setRoles,getRoles};